<?php
namespace App\Models;

class ProjectTempLogFile extends Base {

    public function initialize() {
        parent::initialize();
    }

    public function getSource() {
        return "project_temp_log_file";
    }

    private function t() {
        return __CLASS__;
    }

    /**
     * 获取某个具体的会员营销项目信息
     *
     * @param int $projectId 项目的Id值
     *
     * @return array|false
     */
    public function get($projectId) {
        if($projectId == '' || !is_numeric($projectId)) {
            return false;
        }
        try {
            $query = $this->select('*')->from($this->t())->where('project_id=:project_id:', ['project_id' => $projectId])->limit(1);
            $ret   = $query->getQuery()->execute()->toArray();
            if($ret !== false) {
                if(is_array($ret) && count($ret) > 0) {
                    return $ret[0];
                } else {
                    return [
                        'project_id' => $projectId,
                        'file'       => '',
                        'status'     => 0
                    ];
                }
            }
        } catch(\Exception $ex) {
            $this->errorLog('SELECT', $query->getPhql(), $ex->getMessage(), __FILE__, __LINE__);
        }
        return false;
    }

    /**
     * 编辑会员营销项目信息
     *
     * @param array $entity 信息实体
     *
     * @return int|false
     */
    public function edit($entity) {
        try {
            return $this->db()->updateAsDict($this->getSource(), $entity, sprintf("id=%d LIMIT 1", intval($entity['id'])));
        } catch(\PDOException $ex) {
            $this->errorLog('UPDATE', $this->db()->getSQLStatement(), $ex->getMessage(), __FILE__, __LINE__);
            return false;
        }
    }

    /**
     * 添加会员营销项目信息
     *
     * @param array $entity 信息实体
     *
     * @return int|false
     */
    public function add($entity) {
        try {
            if($this->db()->insertAsDict($this->getSource(), $entity)) {
                return $this->db()->lastInsertId();
            }
        } catch(\PDOException $ex) {
            $this->errorLog('INSERT', $this->db()->getSQLStatement(), $ex->getMessage(), __FILE__, __LINE__);
        }
        return false;
    }
}
